package com.ittest.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ittest.domain.store.Orders;
import com.ittest.domain.store.Traveller;
import com.ittest.factory.MapperFactory;
import com.ittest.mapper.store.OrdersMapper;
import com.ittest.service.store.OrdersService;
import com.ittest.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

public class OrdersServiceImpl implements OrdersService {
    /**
     * 新增订单
     * @param orders    封装好的订单实体类
     */
    @Override
    public void add(Orders orders) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            OrdersMapper ordersMapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
            //设置随机id
            String id = UUID.randomUUID().toString();
            orders.setId(id);
            //调用添加方法
            ordersMapper.add(orders);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }

    }

    @Override
    public void update(Orders orders) {

    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            OrdersMapper ordersMapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
            //处理id
            String[] ids = id.split(",");
            //调用添加方法
            ordersMapper.delete(ids);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
    }

    @Override
    public PageInfo findAll(int page,int size) {
        SqlSession sqlSession = null;
        PageInfo pageInfo = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            OrdersMapper ordersMapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
            //分页
            PageHelper.startPage(page,size);
            //查询所有订单
            List<Orders> list = ordersMapper.findAll();

            //转换下单时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (Orders orders : list) {
                String orderTimeStr = sdf.format(orders.getOrderTime());
                orders.setOrderTimeStr(orderTimeStr);
            }
           //分页对象
            pageInfo = new PageInfo(list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
        return pageInfo;
    }

    @Override
    public Orders findById(String id) {
        SqlSession sqlSession = null;
        Orders orders = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            OrdersMapper ordersMapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
            orders = ordersMapper.findById(id);
           //转换时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //下单时间格式
            String orderTimeStr = sdf.format(orders.getOrderTime());
            orders.setOrderTimeStr(orderTimeStr);
            //出发时间格式
            String departureTimeStr = sdf.format(orders.getProduct().getDepartureTime());
            orders.getProduct().setDepartureTimeStr(departureTimeStr);
            if (orders.getPayType()==0){
                orders.setPayTypeStr("支付宝");
            }else{
                orders.setPayTypeStr("微信");
            }

            List<Traveller> travellers = orders.getTravellers();
            for (Traveller traveller : travellers) {
                //证件类型
                if (traveller.getCredentialsType() == 0){
                    traveller.setCredentialsTypeStr("身份证");
                }
                //人群类型
                if (traveller.getTravellerType() == 1){
                    traveller.setTravellerTypeStr("成年人");
                }else {
                    traveller.setTravellerTypeStr("未成年");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
        return orders;
    }
}
